#!/usr/bin/env python3
## use by redirecting output to a .txt, e.g. colisFoundBugs.txt


import requests
from bs4 import BeautifulSoup

# URL to scrape
url = "https://bugs.debian.org/cgi-bin/pkgreport.cgi?tag=colis-shparser;users=treinen%40debian.org;archive=both"

# Send a GET request to the webpage
response = requests.get(url)

# Parse the webpage content with BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')

# Find all elements with class 'shortbugstatus'
shortbugstatus_elements = soup.find_all(class_='shortbugstatus')

# Loop through each 'shortbugstatus' element
for item in shortbugstatus_elements:

    # Find the bug ID
    bug_link = item.find('a', href=True)
    bug_id = bug_link.text.strip('#').strip() if bug_link else "NA"

    # Find the package name from the hyperlink within the 'submitter' class
    submitter_link = item.find(class_='submitter')
    package_name = submitter_link.text.strip() if submitter_link else "NA" # Package name is the text inside the anchor tag

    # Find the severity 
    severity_chunk = item.find('span', text = lambda x: x and 'Severity' in x) # exists and has Severity in it
    severity = severity_chunk.text.split(':')[-1].strip(";\n").strip(" ") if severity_chunk else "NA" #format is Severity: >code<

    print(f"{bug_id}\t{package_name}\t{severity}")

